package io.gitee.twoke.generator.engine.impl.persistence;

import io.gitee.twoke.generator.engine.impl.interceptor.CommandContext;
import io.gitee.twoke.generator.engine.impl.interceptor.Session;
import io.gitee.twoke.generator.engine.impl.interceptor.SessionFactory;

public class GenericManagerFactory implements SessionFactory {

  protected Class<? extends Session> typeClass;
  protected Class<? extends Session> implementationClass;
  
  public GenericManagerFactory(Class<? extends Session> typeClass, Class<? extends Session> implementationClass) {
    this.typeClass = typeClass;
    this.implementationClass = implementationClass;
  }

  public GenericManagerFactory(Class<? extends Session> implementationClass) {
    this(implementationClass, implementationClass);
  }

  public Class<?> getSessionType() {
    return typeClass;
  }

  public Session openSession(CommandContext commandContext) {
    try {
      return implementationClass.newInstance();
    } catch (Exception e) {
      throw new RuntimeException("couldn't instantiate " + implementationClass.getName() + ": " + e.getMessage(), e);
    }
  }
}